/**
 * Created by lenovo on 2017/11/22.
 */

var downloadCount=0;
$(document).ready(function(){
    addFile();
    $("#fileTable").on("click","tr",function() {
        window.location.href=$(this).attr("data_url");
    });
});

function addFile() {
    $.post("/fs/file/search", {
            pageNum: 1,
            pageSize: 20,
            fileStatus: "审核通过",
            orderType: 1,
            sortType: 0
        },
        function(data,status){
            var dataObject=JSON.parse(data);
            var fileList=dataObject.result;
            var files="";
            for(var i in fileList){
                var type;
                var subFileName;
                if(fileList[i].fileName.indexOf("txt")>=0){
                    type="txt";
                }else if(fileList[i].fileName.indexOf("xls")>=0){
                    type="xls";
                }else if(fileList[i].fileName.indexOf("ppt")>=0){
                    type="ppt";
                }else if(fileList[i].fileName.indexOf("pdf")>=0){
                    type="pdf";
                }else{
                    type="doc";
                }
                if(fileList[i].fileName.replace(/[^\x00-\xff]/g,"01").length>27){
                    subFileName=fileList[i].fileName.substring(0,24)+"...";

                }else{
                    subFileName=fileList[i].fileName;
                }
                $.ajax({
                    url: 'file/download_count',
                    type: 'get',
                    data: {
                        fileId: fileList[i].id
                    },
                    cache: false,
                    async:false,
                    success: function (data) {
                        downloadCount=JSON.parse(data).count.toString();
                    }
                });
                files+="<tr class='fileItem' data_url=detail.html?fileId="+fileList[i].id+"><td class='file_icon "+type+"'></td><td class='file_info'>"+
                    "<p class='file_title'>"+subFileName+"</p>"+
                    " <p class='file_label'>"+fileList[i].browseCount+"阅读&nbsp;&nbsp;"+downloadCount+"下载</p></td></tr>";
            }
            $("#fileTable").html(files);
        });
}